package fr.upmc.i3s.upmcontact.content.containers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import fr.upmc.i3s.upmcontact.contents.LostFoundObject;
import fr.upmc.i3s.upmcontact.contents.Published;
import android.content.Context;

public class CollectedContentManager {

	  ///////////////
	 // SINGLETON //
	///////////////
	
	private static volatile CollectedContentManager INSTANCE;
	
	public static CollectedContentManager getInstance(Context context) {
		if(INSTANCE == null) {
			INSTANCE = new CollectedContentManager(context); 
		}
		return INSTANCE;
	}
	
	/////////////////////////////////////////////////////////////////
	
	private Context context;
	private FoundObjectsContainer foundObjects;
	private LostObjectsContainer lostObjects;
	private MerchantsInfosContainer merchantInfos;
	private UpmcInfosContainer upmcInfos;
	private StudentLifeInfosContainer studentLifeInfos;
	private ChatMessagesContainer chatMessages;
	
	private CollectedContentManager(Context context) {
		this.context = context;
		this.foundObjects = new FoundObjectsContainer();
		this.lostObjects = new LostObjectsContainer();
		this.merchantInfos = new MerchantsInfosContainer();
		this.upmcInfos = new UpmcInfosContainer();
		this.studentLifeInfos = new StudentLifeInfosContainer();
		this.chatMessages = new ChatMessagesContainer();
	}
	
	/////////////////////////////////////////////////////////////////
	
	public Published getRandomRecentInformation() {
		Published result = null;
		Date date = new Date(0);
		Random r = new Random();
		int valeur = r.nextInt(3);
		if(valeur == 0) {
			for(Published p : merchantInfos) {
				if(p.getCreatedAt().after(date)) {
					result = p;
					date = p.getCreatedAt();
				}
			}
		} else if (valeur == 1) {
			for(Published p : upmcInfos) {
				if(p.getCreatedAt().after(date)) {
					result = p;
					date = p.getCreatedAt();
				}
			}
		} else if (valeur == 2) {
			for(Published p : studentLifeInfos) {
				if(p.getCreatedAt().after(date)) {
					result = p;
					date = p.getCreatedAt();
				}
			}
		}
		return result;
	}
	
	
	public List<LostFoundObject> getRecentLostObjects(int quantity) {
		List<LostFoundObject> result = new ArrayList<LostFoundObject>();
		LostObjectsContainer tempLOC = new LostObjectsContainer();
		tempLOC.addAll(lostObjects);
		// set max 
		int max = 0;
		if(tempLOC.size() > quantity) { max = quantity; }
		else { max = tempLOC.size(); }
		// get the last lost objects
		for(int i=0; i<max; i++) {
			Date date = new Date(0);
			LostFoundObject lastLfo = null;
			for(LostFoundObject lfo : tempLOC) {
				if(lfo.getCreatedAt().after(date)) {
					lastLfo = lfo;
					date = lfo.getCreatedAt();
				}
			}
			result.add(lastLfo);
			tempLOC.remove(lastLfo);
		}
		return result;
	}
	
	
	public List<LostFoundObject> getRecentFoundObjects(int quantity) {
		List<LostFoundObject> result = new ArrayList<LostFoundObject>();
		FoundObjectsContainer tempFOC = new FoundObjectsContainer();
		tempFOC.addAll(foundObjects);
		// set max 
		int max = 0;
		if(tempFOC.size() > quantity) { max = quantity; }
		else { max = tempFOC.size(); }
		// get the last lost objects
		for(int i=0; i<max; i++) {
			Date date = new Date(0);
			LostFoundObject lastLfo = null;
			for(LostFoundObject lfo : tempFOC) {
				if(lfo.getCreatedAt().after(date)) {
					lastLfo = lfo;
					date = lfo.getCreatedAt();
				}
			}
			result.add(lastLfo);
			tempFOC.remove(lastLfo);
		}
		return result;
	}
	
	
	/////////////////////////////////////////////////////////////////
	
	public void loadAllSavedStates() {
		this.foundObjects.loadSavedState(context);
		this.lostObjects.loadSavedState(context);
		this.merchantInfos.loadSavedState(context);
		this.upmcInfos.loadSavedState(context);
		this.studentLifeInfos.loadSavedState(context);
		this.chatMessages.loadSavedState(context);
	}
	
	public void saveAllStates() {
		this.foundObjects.saveState(context);
		this.lostObjects.saveState(context);
		this.merchantInfos.saveState(context);
		this.upmcInfos.saveState(context);
		this.studentLifeInfos.saveState(context);
		this.chatMessages.saveState(context);
	}
	
	
	/////////////////////////////////////////////////////////////////

	public FoundObjectsContainer getFoundObjects() {
		return foundObjects;
	}

	public LostObjectsContainer getLostObjects() {
		return lostObjects;
	}

	public MerchantsInfosContainer getMerchantsInfos() {
		return merchantInfos;
	}

	public UpmcInfosContainer getUpmcInfos() {
		return upmcInfos;
	}

	public StudentLifeInfosContainer getStudentLifeInfos() {
		return studentLifeInfos;
	}
	
	public ChatMessagesContainer getChatMessages() {
		return chatMessages;
	}

}
